﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Speech.Synthesis;
using Aspose.Words;
using Aspose.Pdf;
using Aspose.Pdf.Text;
using Aspose.Slides;
using Aspose.Slides.Util;

namespace Aspose.Listener
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SpeechSynthesizer voice = new SpeechSynthesizer();
    

        private void Browse_Click(object sender, EventArgs e)
        {
            try
            {

                OpenFileDialog ofd = new OpenFileDialog();

                ofd.CheckFileExists = true;
                ofd.CheckPathExists = true;
                ofd.DefaultExt = "txt";
                ofd.DereferenceLinks = true;
                ofd.Filter = "Text files (*.txt)|*.txt|" + "RTF files (*.rtf)|*.rtf|" + "Word document (*.doc)|*.doc|" + "Words document (*.docx)|*.docx|" + "PDF document (*.pdf)|*.pdf|" + "Presentation document (*.ppt)|*.ppt|"+ "Presentation document (*.pptx)|*.pptx";
                ofd.Multiselect = false;
                ofd.RestoreDirectory = true;
                ofd.ShowHelp = true;
                ofd.ShowReadOnly = false;
                ofd.Title = "Select a File";
                ofd.ValidateNames = true;
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    string ext = Path.GetExtension(ofd.FileName);// checks file extension

                    if (ext == ".docx" || ext == ".doc")// for doc file formats
                    {

                        Aspose.Words.Document doc = new Aspose.Words.Document(ofd.FileName);
                        richTextBox1.Text = doc.GetText();// gets text from doc/docx file and assig text to the text box
                    }
                    else if (ext == ".pdf")// for pdf file formats
                    {
                        //open document
                        Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(ofd.FileName);
                        //create TextAbsorber object to extract text
                        TextAbsorber textAbsorber = new TextAbsorber();
                        //accept the absorber for all the pages
                        pdfDocument.Pages.Accept(textAbsorber);
                        string extractedText = textAbsorber.Text;// gets text from pdf file 
                        richTextBox1.Text = extractedText;//  assig text to the text box


                    }
                    else if (ext == ".ppt" || ext == ".pptx")// for power point files
                    {
                        Presentation pres = new Presentation(ofd.FileName);
                        string sldtxt = "";
                        //Get all text boxes in the presentation
                        ITextFrame[] tb = SlideUtil.GetAllTextFrames(pres, true);
                        for (int i = 0; i < tb.Length; i++)
                            foreach (Aspose.Slides.Paragraph para in tb[i].Paragraphs)
                                foreach (Portion port in para.Portions)
                                    //Find text to be replaced
                                    
                                    //Replace exisitng text with the new text
                                    {
                                        sldtxt = sldtxt + port.Text;// gets text from ppt/pptx file 
                                       }
                        richTextBox1.Text = sldtxt;//  assig text to the text box
                       
                    }
                    else if (ext == ".txt" || ext == ".rtf")// for text and rich text files
                    {
                        StreamReader sr = new StreamReader(ofd.OpenFile());
                        richTextBox1.Text = sr.ReadToEnd();
                    }

                    else
                    {
                        MessageBox.Show("Sorry! " + ext + " file format not supported");// displays message for file format not supported
                    }
                }

            }
            catch (Exception)
            {
                MessageBox.Show("Cannot Open Selected File", "Aspose Listener", MessageBoxButtons.OK, MessageBoxIcon.Error);//error displayed if error generated during file upload
            }

        }

        private void Speak_Click(object sender, EventArgs e)
        {

            // start voice function 


            try
            {
               
               voice.SelectVoiceByHints(VoiceGender.NotSet);// setting voice type
               voice.Volume = trackBar1.Value;// setting volume
               


                voice.SpeakAsync(richTextBox1.Text);// starts sound for text displayed in text box
                Pause.Enabled = true;// pause button enable
               

            }
            catch (Exception)
            {
                MessageBox.Show("Voice Error", "Aspose Listener", MessageBoxButtons.OK, MessageBoxIcon.Error);// display error message in case problem in voice function
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            richTextBox1.Text = "Load File from Browse to Listen";// Intializes text box
            Pause.Enabled = false; // making pause button disable
            Resume.Enabled = false; // making resume button disable
        }

        private void Pause_Click(object sender, EventArgs e)
        {

            voice.Pause(); // pause the voice
            Resume.Enabled = true; //making resume button enable
           
        }

        private void Resume_Click(object sender, EventArgs e)
        {
            voice.Resume(); //resume the voice
        }

        

        private void save_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();// instializes wav file save dialog

            sfd.Filter = "All files (*.*)|*.*|wav files (*.wav)|*.wav";
            sfd.Title = "Save to a wave file";
            sfd.FilterIndex = 2;
            sfd.RestoreDirectory = true;

            if (sfd.ShowDialog() == DialogResult.OK)// broswe the location to save wav file
            {
                FileStream fs = new FileStream(sfd.FileName, FileMode.Create, FileAccess.Write);
                voice.SetOutputToWaveStream(fs);
                voice.Speak(richTextBox1.Text);
                fs.Close();
                MessageBox.Show("File Saved Sucessfully");// displays message after file saved option is completed
            }
           

        }

        private void clear_Click(object sender, EventArgs e)
        {
            voice.SpeakAsyncCancelAll();// cancell the voice
            richTextBox1.Text = "";// empty text box
        }

        private void Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();// exit the application window
        }

       
    }
}
